package com.cruisecontrol4eclipse.core.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Date;

public class CruiseControlProject {

	PropertyChangeSupport propertySupport = new PropertyChangeSupport(this);

	String name;
	String label;
	Status status;
	String lastBuild;
	String lastSuccessfulBuildLabel;
	String lastSuccessfulBuild;
	Date buildStartTime;
	Boolean paused;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		propertySupport.firePropertyChange("name", this.name, this.name = name);	
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		propertySupport.firePropertyChange("label", this.label,
				this.label = label);
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		propertySupport.firePropertyChange("status", this.status,
				this.status = status);
	}

	public String getLastBuild() {
		return lastBuild;
	}

	public void setLastBuild(String lastBuild) {
		propertySupport.firePropertyChange("lastBuild", this.lastBuild,
				this.lastBuild = lastBuild);
	}

	public String getLastSuccessfulBuildLabel() {
		return lastSuccessfulBuildLabel;
	}

	public void setLastSuccessfulBuildLabel(String lastSuccessfulBuildLabel) {
		propertySupport.firePropertyChange("lastSuccessfulBuildLabel", this.lastSuccessfulBuildLabel,
				this.lastSuccessfulBuildLabel = lastSuccessfulBuildLabel);
	}

	public String getLastSuccessfulBuild() {
		return lastSuccessfulBuild;
	}

	public void setLastSuccessfulBuild(String lastSuccessfulBuild) {
		propertySupport.firePropertyChange("lastSuccessfulBuild", this.lastSuccessfulBuild,
				this.lastSuccessfulBuild = lastSuccessfulBuild);
	}

	public Date getBuildStartTime() {
		return buildStartTime;
	}

	public void setBuildStartTime(Date buildStartTime) {
		propertySupport.firePropertyChange("buildStartTime", this.buildStartTime,
				this.buildStartTime = buildStartTime);
	}

	public Boolean getPaused() {
		return paused;
	}

	public void setPaused(Boolean paused) {
		propertySupport.firePropertyChange("paused", this.paused,
				this.paused = paused);
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		propertySupport.addPropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		propertySupport.removePropertyChangeListener(listener);
	}

	public enum Status {
		IN_BUILD_QUEUE("queued","in build queue"),
		WAITING_FOR_NEXT_BUILD("waiting","waiting for next time to build"),
		BOOTSTRAPPING("bootstrapping", "bootstrapping"),
		IDLE("idle", "Idle"),
		CHECKING_FOR_MODIFICATIONS("modificationset", "checking for modifications"),
		NOW_BUILDING("building", "now building"),
		MERGING_LOG_FILES("merging","merging accumulated log files"),
		PUBLISHING_RESULTS("publishing", "publishing build results"),
		PAUSED("paused","Is paused");
		
		private final String text;
		private final String key;

		Status(String key, String text) {
			this.key = key;
			this.text = text;
		}

		@Override
		public String toString() {
			return text;
		}
		
		public String getKey(){
			return key;
		}
		
		public static Status getStatusFromKey(String value) {
			for (Status status : Status.values()) {
				if(value.startsWith(status.getKey())) {
					return status;
				}
			}
			return getStatusFromText(value);
		}
		
		public static Status getStatusFromText(String value) {
			for (Status status : Status.values()) {
				if(value.startsWith(status.toString())) {
					return status;
				}
			}
			return null;
		}
	}
}
